package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.NoticeBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class NoticePresenter extends basePresenter {

    private Contract.NoticeView noticeView;

    public NoticePresenter(Contract.NoticeView view) {
        super(view);
        this.noticeView = view;
    }

    public void initData(int page, String type,int state, final boolean isRefresh, boolean isShowDialog) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.noticeMessage);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("type", type);
        rp.addBodyParameter("page", page + "");
        rp.addBodyParameter("state", state + "");
        x.http().post(rp, new myCallback(BaseActivity.context, NoticeBean.class, isShowDialog, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                NoticeBean bean = (NoticeBean) baseBean;
                if (isRefresh) {
                    noticeView.onRefresh(bean);
                } else {
                    noticeView.loadMore(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                noticeView.onFail();
            }
        });
    }

    public void read(String id, final int position) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.noticeMessageSave);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        if (position != -1) {
            rp.addBodyParameter("id", id);
        }
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                noticeView.readSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }

    public void delete(String id, final int position) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.noticeMessageDelete);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id", id);
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                noticeView.deleteSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
}
